package com.cfs.view;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Administrator on 2017/9/27/027.
 */
@Controller
public class LoginController {

    @RequestMapping(value = "/login")
    public String login(){
        return "login/login";
    }


    @RequestMapping(value = "/logins")
    public String logins(HttpServletRequest request,String username){
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken();
        usernamePasswordToken.setUsername(username);
        usernamePasswordToken.setPassword("xxx".toCharArray());
        usernamePasswordToken.setHost("localhost");
        SecurityUtils.getSubject().login(usernamePasswordToken);


        Subject subject= SecurityUtils.getSubject();


        System.out.println(subject.isAuthenticated());

//        Session session = subject.getSession(false);
//        session.setAttribute("xxx", "xxx");
        SavedRequest srq = WebUtils.getAndClearSavedRequest(request);
        if(srq != null){
            return "redirect:" + srq.getRequestUrl();
        }

        return "redirect:/index";
    }


}
